package com.tomcat.mesos.client;

import com.tomcat.mesos.Config;
import com.tomcat.mesos.tomcat.impl.TomcatInstanceManager;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * tomcat on Mesos 框架
 *
 * @author Rayn on 2015/12/21 15:03
 * @email liuwei412552703@163.com
 */
public class Client
{
    private static final Logger LOG = LoggerFactory.getLogger(Client.class);

    /**
     * tomcat 命令行参数配置
     */
    private Config tomCmdConfig;
    private Properties props;

    public Client(Config tomcatConfig, Properties conf)
    {
        this.tomCmdConfig = tomcatConfig;
        this.props = conf;
    }

    /**
     * 帮助信息
     */
    private void printUsage(){
        System.out.println("Usage : Client <master>");
    }

    /**
     * 启动类
     * @param args
     */
    public static void main(String[] args)
    {
        final Config tomCmdConfig = Config.parse(args);

        final Client client = new Client(tomCmdConfig, getDefaultConfiguration());
        switch (tomCmdConfig.command){
            case START:
                client.start();
                break;
            case ADD:
                client.add();
            case STOP:
                client.stop();
                break;
            case STATUS:
                client.stats();
                break;
            default:
                client.printUsage();
                break;
        }
    }

    /**
     * 系统默认配置信息
     */
    private static Properties getDefaultConfiguration()
    {
        Properties props = new Properties();
        try
        {
            InputStream stream = Client.class.getClass().getClassLoader().getResourceAsStream("tomcat.properties");
            props.load(stream);
        }
        catch(FileNotFoundException e){
            LOG.error("tomcat properties 文件未在 classpath 中找到");
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return props;
    }

    /**
     * 启动应用
     */
    public void start(){

    }

    /**
     * 添加tomcat 实例
     */
    private void add()
    {

    }

    /**
     * 停止一个实例
     */
    private void stop(){
        final CuratorFramework zookeeper = CuratorFrameworkFactory.newClient(tomCmdConfig.zookeeper, new RetryOneTime(10000));
        zookeeper.start();
        try
        {
            TomcatInstanceManager.stop(zookeeper);
        }
        catch (KeeperException.NoNodeException e)
        {
            System.out.println(" * No service registered *");
            LOG.error(" 没有 tomcat 实例进行注册.");
        }  finally
        {
            zookeeper.close();
        }
    }

    /**
     * 获取某实例当前状态
     */
    private void stats(){
        final CuratorFramework zookeeper = CuratorFrameworkFactory.newClient(tomCmdConfig.zookeeper, new RetryOneTime(10000));
        zookeeper.start();
        try
        {
            TomcatInstanceManager.status(zookeeper);
        }
        catch (KeeperException.NoNodeException e)
        {
            System.out.println(" * No service registered *");
            LOG.error(" 没有 tomcat 实例进行注册.");
        }  finally
        {
            zookeeper.close();
        }
    }
}
